package com.csxy.lxl.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.lxl.R;
import com.csxy.lxl.entity.BannerQuery;
import com.csxy.lxl.entity.CrmBanner;
import com.csxy.lxl.service.CrmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cmsservice/banner")
public class CrmBannerController {
    @Autowired
    private CrmService crmService;

    //条件分页查询banner
    @PostMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable Long page, @PathVariable Long limit,
                        @RequestBody(required = false) BannerQuery bannerQuery){
        Page<CrmBanner> bannerPage= new Page<>(page,limit);
        crmService.pageQuery(bannerPage,bannerQuery);
        //获取数据
        List<CrmBanner> list = bannerPage.getRecords();
        //获取总记录数
        long total = bannerPage.getTotal();
        return R.ok().data("rows",list).data("total",total);
    }


    //添加轮播图
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        boolean save = crmService.save(crmBanner);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }


    //修改轮播图
    @PostMapping("updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        boolean update = crmService.updateById(crmBanner);
        if(update){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //删除轮播图
    @DeleteMapping("deleteBannerById/{id}")
    public R deleteBanner(@PathVariable("id") String id){
        boolean flag = crmService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //根据id查询轮播图
    @GetMapping("getBannerById/{id}")
    public R getBannerById(@PathVariable("id") String id){
        CrmBanner banner = crmService.getById(id);
        return R.ok().data("banner",banner);
    }

}
